package com.producer.model.table;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

/**
 * 消息表
 *
 * @author Liunh
 */
@DynamicInsert
@DynamicUpdate
@Entity
@Table(name = "SysMessage")
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class SysMessage implements Serializable {

    @Transient
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @ApiModelProperty(name = "id", value = "id", example = "1")
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 信息来源
     */
    @ApiModelProperty(name = "source", value = "信息来源", example = "101001010000")
    @Column(name = "source")
    private Long source;

    /**
     * 消息名
     */
    @ApiModelProperty(name = "title", value = "消息名", example = "消息名")
    @Column(name = "title")
    private String title;

    /**
     * 消息体数据
     */
    @ApiModelProperty(name = "data", value = "消息体数据")
    @Column(name = "data")
    private String data;

    /**
     * 发送目的地
     */
    @ApiModelProperty(name = "target", value = "发送目的地", example = "101001010010")
    @Column(name = "target")
    private String target;

    /**
     * 批次号
     * 一次发送或者一次接收
     */
    @ApiModelProperty(name = "batchNo", value = "批次号, 一次发送或者一次接收, 定时任务发送和接收消息才有值")
    @Column(name = "batchNo")
    private String batchNo;

    /**
     * 消息类型(0 站内信(通知)； 1 短信； 2 邮件； 3 数据收集)
     */
    @ApiModelProperty(name = "msgType", value = "消息类型(0 站内信(通知)； 1 短信； 2 邮件； 3 数据收集)")
    @Column(name = "msgType")
    private Integer msgType;

    /**
     * 数据类型（0普通消息、1图书数据、2读者数据、3借阅数据、4电子书数据）
     */
    @ApiModelProperty(name = "dataType", value = "数据类型（0普通消息、1图书数据、2读者数据、3借阅数据、4电子书数据）")
    @Column(name = "dataType")
    private Integer dataType;

    /**
     * 系统定时任务发送状态
     * 0未发送,1已发送
     */
    @ApiModelProperty(name = "status", value = "系统定时任务发送状态：0未发送,1已发送", example = "0")
    @Column(name = "status")
    private Integer status;

    /**
     * 发送时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(name = "sendTime", value = "发送时间")
    @Column(name = "sendTime")
    private Date sendTime;

    @ApiModelProperty(name = "userId", value = "创建人id", example = "1")
    @Column(name = "userId")
    private Long userId;

    @ApiModelProperty(name = "receiverId", value = "接收人id", example = "1,2")
    @Column(name = "receiverId")
    private String receiverId;

    @ApiModelProperty(name = "typeNid", value = "类型与接收方id：机构0，用户1", example = "0_1,1_1")
    @Column(name = "typeNid")
    private String typeNid;

    @ApiModelProperty(name = "updateUserId", value = "修改人id", example = "1")
    @Column(name = "updateUserId")
    private Long updateUserId;

    /**
     * 消息描述
     */
    @ApiModelProperty(name = "descr", value = "消息描述", example = "1")
    @Column(name = "descr")
    private String descr;

    /**
     * 消息创建时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(name = "createTime", value = "消息创建时间")
    @Column(name = "createTime")
    private Date createTime;

    /**
     * 修改时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(name = "updateTime", value = "修改时间")
    @Column(name = "updateTime")
    private Date updateTime;

//    @OneToOne(targetEntity = xiaohua.books.manage.common.table.User.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
//    @PrimaryKeyJoinColumn(name = "userId", referencedColumnName = "id")
//    private User user;
//
//    @OneToOne(targetEntity = xiaohua.books.manage.common.table.Organization.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
//    @JoinColumn(name = "source", referencedColumnName = "orgCode", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT), insertable = false, updatable = false)
//    private Organization sourceOrg;
//
//    @OneToOne(targetEntity = xiaohua.books.manage.common.table.Organization.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
//    @JoinColumn(name = "target", referencedColumnName = "orgCode", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT), insertable = false, updatable = false)
//    private Organization targetOrg;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
            return false;
        }
        SysMessage that = (SysMessage) o;
        return id != null && Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
